package com.wzy.flowable.flow.Controller;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wzy
 * @Date: 2022/03/20/15:54
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("/wzy")
public class Test01 {
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;

    /**
     * 部署
     */
    @RequestMapping("/test")
    public String test() {
        // 1. 回获取ProcessEngine
        // 2. 获取RepositoryService
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("holiday_request.bpmn20.xml")
                .name("请假流程")
                .deploy();
        return deploy.getId();
    }

    /**
     * 查询流程定义的信息
     *
     * @return
     */
    @RequestMapping("/query")
    public String query(String id) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
        log.info(processDefinition.toString());
        return processDefinition.getKey();

    }

    /**
     * 删除流程
     *
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        repositoryService.deleteDeployment(id, true);
        return "删除成功！";
    }

    /**
     * 启动流程
     *
     * @param id
     * @return
     */
    @RequestMapping("/start")
    public String start(String id) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("levle", "高级");
        map.put("employee", "zhangsan");
        map.put("nrOfHolidays", 10);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey(), map);
        return processInstance.getId();
    }

    @RequestMapping("/approval01")
    public String approval01(String processInstanceIdId, String assignee) {
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceIdId).singleResult();
        List<Task> list = taskService.createTaskQuery().processDefinitionKey(processInstance.getProcessDefinitionKey()).taskAssignee(assignee).list();
        for (Task task : list) {
            System.out.println(task.toString());
        }
        return list.get(list.size() - 1).getId();
    }

    @RequestMapping("/testCompleteTask")
    public String testCompleteTask(String taskId) {
        Task task = taskService.createTaskQuery().taskAssignee("zhangsan").taskId(taskId).singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("approved", false);
        TaskService taskService = processEngine.getTaskService();
        taskService.complete(task.getId(), map);
        return "审批结束";
    }

    @RequestMapping("/history")
    public String history(String processInstanceId) {
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).orderByHistoricActivityInstanceEndTime().desc()
                .list();
        for (HistoricActivityInstance historicActivityInstance : list) {
            System.out.println(historicActivityInstance.toString());
        }
        return "successs";
    }


}
